package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.Map;

/**
 * @author dyqstart
 * @create 2021-09-03-17:20
 * @since 1.0.0
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    LogService logService;

    @PostMapping("/list")
    public Map<String,Object> getCustomerList(Integer page,
                                              Integer rows,
                                              String customerName){

        Map<String,Object> map = customerService.getCustomerList(page,rows,customerName);
        Log log = new Log(Log.SELECT_ACTION,"查询客户列表");
        logService.save(log);
        return map;
    }

    @PostMapping("/save")
    public ServiceVO saveCustomer(Customer customer,
                                  @PathParam("customerId") Integer customerId){
        customerService.saveOrUpdateByCustomerId(customer,customerId);
        Log log = new Log(Log.INSERT_ACTION,"新增客户");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }

    @PostMapping("/delete")
    public ServiceVO deleteCustomerByIds(String ids){
        customerService.deleteCustomerByIds(ids);
        Log log = new Log(Log.DELETE_ACTION,"删除客户");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }
}

